package cn.wolfcode.controller;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery qo){
        Page<Region> page = regionService.listByQuery(qo);
        model.addAttribute("page",page);

        return "/region/list";
    }

    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        List<Destination> list = destinationService.list();
        return list;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Region region){
        if (region.getId()==null){
            regionService.save(region);
        }else {
            regionService.update(region);
        }
        return JsonResult.success();
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id,int hot){
        regionService.changeHot(id,hot);
        return JsonResult.success();
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> destinations = destinationService.queryByRegionId(rid);
        //System.out.println(destinations);
        return destinations;
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        List<Destination> destinations = destinationService.getDestByDeep(deep);
        return destinations;
    }



}
